package com.example.cursor.controller;

import com.example.cursor.common.Result;
import com.example.cursor.dto.LoginDTO;
import com.example.cursor.entity.User;
import com.example.cursor.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Result<User> login(@RequestBody LoginDTO loginDTO, HttpSession session) {
        User user = userService.login(loginDTO);
        // 将用户信息存入session
        session.setAttribute("user", user);
        return Result.success(user);
    }
    
    @PostMapping("/logout")
    public Result<Void> logout(HttpSession session) {
        session.removeAttribute("user");
        return Result.success(null);
    }
}